package com.offcn.dao.Impl;

import com.offcn.bean.User;
import com.offcn.dao.UserDao;
import com.offcn.service.Impl.UserServiceImpl;
import com.offcn.service.UserService;
import com.offcn.utils.BaseDao;
import com.offcn.utils.GetNewTime;
import com.offcn.utils.JdbcUtil;
import com.offcn.utils.Page;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

/**
 * @Author: wy
 * @Date: 2022/9/5 19:38
 */
public class UserDaoImpl extends BaseDao<User> implements UserDao {

    //判断 用户是否登陆的
    @Override
    public User doLogin(String username, String password) {
        String sql = "select * from `user` where username=? and `password`=?";
        User bean = getBean(sql, username, password);
        return bean;
    }

    @Override
    public Page<User> getUserListPage(int pno, int psize, String name) {

        Page<User> page = new Page<>();
        page.setPageNo(pno);

        page.setPageSize(psize);
        int userCount = getUserCount(name);
        page.setTotalRecord(userCount);

        String sql = "select * from user where name like ? limit ?,?";
        List<User> beanList =
                getBeanList(sql, "%" + name + "%", (page.getPageNo() - 1) * page.getPageSize(), page.getPageSize());

        page.setList(beanList);

        return page;
    }

    @Override
    public int getUserCount(String name) {
        String sql = "select count(*) from user where name like ?";
        Long singleValue = (Long) getSingleValue(sql, "%" + name + "%");
        return singleValue.intValue();
    }

    @Override
    public int AddUser(User user) {
        String sql = "INSERT INTO `user` (`name`,phone,age,sex,username," +
                "`password`,`status`,createtime,role) VALUES (?,?,?,?,?,?,?,?,?)";
        Object[] objects = {user.getName(), user.getPhone(), user.getAge(), user.getSex(),
                user.getUsername(), user.getPassword(), user.getStatus(), user.getCreatetime()
                , user.getRole()};
        int update = update(sql, objects);
        return update;
    }

    @Override
    public int deleteUser(String uids) {

        String[] split = uids.split(",");
        int update = 0;
        Connection connection = null;
        PreparedStatement preparedStatement01 = null;
        PreparedStatement preparedStatement02 = null;
        try {
            connection = JdbcUtil.getConnection();
            //手动提交事务
            connection.setAutoCommit(false);
            //先删除课程用户中间表的数据
            for (String uid : split) {
                String sql01 = "DELETE FROM `course_user` WHERE uid=?";
                preparedStatement01 = connection.prepareStatement(sql01);
                preparedStatement01.setObject(1, uid);
                preparedStatement01.executeUpdate();
            }
            //再删除用户表的数据
            String sql02 = "DELETE FROM `user` WHERE uid IN (" + uids + ")";
            preparedStatement02 = connection.prepareStatement(sql02);
            update = preparedStatement02.executeUpdate();
            connection.commit();
        } catch (SQLException e) {
            try {
                //如果出现删除异常那么事务回滚
                connection.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        } finally {
            try {
                if (preparedStatement01 != null) {
                    preparedStatement01.close();
                }
                if (preparedStatement02 != null) {
                    preparedStatement02.close();
                }
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return update;
    }

    //修改用户
    @Override
    public int updateUser(User user) {
        String sql = "update user set `name`=?,phone=?,age=?,sex=?,`password`=?,`status`=?,role=? where uid=?";
        Object[] objects = {user.getName(), user.getPhone(), user.getAge(), user.getSex(),
                user.getPassword(), user.getStatus(), user.getRole(), user.getUid()};
        int update = update(sql, objects);
        return update;
    }

    @Override
    public User getUserByUsername(String username) {

        String sql = "SELECT * from user where username=?";
        User bean = getBean(sql, username);
        return bean;
    }

    @Override
    public User doExistPhone(String phone) {
        String sql = "SELECT * from user where phone=?";
        User bean = getBean(sql, phone);
        return bean;
    }


    public static void main(String[] args) {
        UserDao userDao = new UserDaoImpl();
//        User user = userService.doLogin("root", "123456");
//        System.out.println(user);

        userDao.updateUser(new User("王昱", "15002988071", 23, 1, "w", "www", null, 2, GetNewTime.getNewTime(), 1));
    }
}
